package org.csmzxy.class233;

class YieldThread extends Thread{
    public YieldThread(String name){
        super(name);
    }
    @Override
    public void run(){
        for (int i = 1; i < 4; i++){
            String threadName = Thread.currentThread().getName();
            if(i == 2){
                System.out.println(threadName + "线程让步!");
                Thread.yield();   //线程运行到此处时做出让步
            }
            System.out.println(threadName + "---" + i);
        }
    }
}
public class Example05 {
    public static void main(String[] args){
        //创建两个线程对象
        Thread thread1 = new YieldThread("thread1");
        Thread thread2 = new YieldThread("thread2");
        //启动两个线程
        thread1.start();
        thread2.start();
    }
}
